<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @abstract 同步订单
 * @author zxj
 * @description
 */
class SyncOrder extends CI_Controller
{
    private $debug = false;

    function __construct()
    {
        parent::__construct();
        $this->load->model('salesforce_model', 'sfModel');
        $this->debug = gp('debug');
        if ($this->debug == 'true') {
            $this->output->enable_profiler(TRUE);
        }
    }


    /**
     * 同步订单到SF
     * @param string $oid
     * @return bool
     */
    function sync_order_sf($oid = '')
    {
        //同步初始订单
        $where = $oid ? array('id' => $oid) : array('isSync_sf' => 0,'syncsf_error is Null'=>Null);;
        $orders = $this->common_model->select('mt_order', 0, 50, 'id desc', $where);
        foreach ($orders as $rs) {
            $result = $this->sfModel->oms_to_sf001($rs['id']);
            if ($result['status']) {
                $this->common_model->update('mt_order', array('isSync_sf' => 1), array('id' => $rs['id']));
            } else {
                $data = array(
                    'title' => 'oms_to_sf001 error:', 'order ID' => $rs['id'], 'errorData' => $result
                );
                $this->common_model->update('mt_order', array('syncsf_error' => $result['message']), array('id' => $rs['id']));
                log_output($data);
                if ($this->debug){
                    gsh_output($data);
                }
            }
        }
        //同步轨迹
        $where = $oid ? array('oid' => $oid) : array('is_sync' => 0);
        $tracks = $this->common_model->selectCustom('mt_order_track', $where);
        foreach ($tracks as $rs) {
            $oid = $rs['oid'];
            $orderOne = $this->common_model->selectFirst('mt_order',array('id'=>$oid));
            if($orderOne['syncsf_error'])return false;
            $result = $this->sfModel->oms_to_sf002($oid);
            if ($result['status']) {
                $this->common_model->update('mt_order_track', array('is_sync' => 1), array('id' => $rs['id']));
            } else {
                $data = array(
                    'title' => 'oms_to_sf002 error', 'order ID' => $oid, 'errorData' => $result
                );
                log_output($data);
                if ($this->debug){
                    gsh_output($data);
                }
            }
            //推送报价
            if ($rs['end_sid'] == 3) {
                $result = $this->sfModel->oms_to_sf001($oid);
                if (!$result['status']) {
                    $data = array(
                        'title' => 'oms_to_sf001 push quote error', 'order ID' => $oid, 'errorData' => $result
                    );
                    log_output($data);
                    if ($this->debug){
                        gsh_output($data);
                    }
                }
            }
        }
    }


    /**
     * 同步产品到SF
     * @param string $oid
     * @return bool
     */
    function sync_product_sf($oid = '')
    {
        //同步轨迹
        $where = $oid ? array('oid' => $oid) : array('is_sync' => 0);
        $tracks = $this->common_model->selectCustom('mt_product_track', $where);
        $pushRule = array(2,11,5);
        foreach ($tracks as $rs) {
            $pid = $rs['pid'];
            $productOne = $this->common_model->selectFirst('mt_product',array('id'=>$pid));
            if(!$productOne){
                continue;
            }
            $orderOne = $this->common_model->selectFirst('mt_order',array('id'=>$rs['oid']));
            if($orderOne['syncsf_error']){
                continue;
            }
            $result = $this->sfModel->oms_to_sf004($pid);
            if ($result['status']) {
                $this->common_model->update('mt_product_track', array('is_sync' => 1), array('id' => $rs['id']));
            } else {
                $data = array(
                    'title' => 'oms_to_sf004 error:', 'product ID' => $pid, 'errorData' => $result
                );
                log_output($data);
                if ($this->debug){
                    gsh_output($data);
                }
            }
            //推送产品信息
            if (in_array($rs['end_sid'], $pushRule)) {
                $result = $this->sfModel->oms_to_sf003($pid);
                if (!$result['status']) {
                    $data = array(
                        'title' => 'oms_to_sf003 error', 'product ID' => $pid, 'errorData' => $result
                    );
                    log_output($data);
                    if ($this->debug){
                        gsh_output($data);
                    }
                }
            }
        }

    }
	
    /**
     * 同步批量单到SF
     * @param string $oid
     * @return bool
     */
    function sync_multiorder_sf($oid = '')
    {
        //同步初始订单
        $where = $oid ? array('id' => $oid) : array('is_sf' => 0,'syncsf_error is Null'=>Null);;
        $orders = $this->common_model->select('mt_multi_orders', 0, 50, 'id desc', $where);
        foreach ($orders as $rs) {
            $result = $this->sfModel->oms_to_sf001_multiorder($rs['id']);
            if ($result['status']) {
                $this->common_model->update('mt_multi_orders', array('is_sf' => 1), array('id' => $rs['id']));
            } else {
                $data = array(
                    'title' => 'oms_to_sf001 error:', 'order ID' => $rs['id'], 'errorData' => $result
                );
                $this->common_model->update('mt_multi_orders', array('syncsf_error' => $result['message']), array('id' => $rs['id']));
                log_output($data);
                if ($this->debug){
                    gsh_output($data);
                }
            }
        }
        
    }

}
